/*
Copyright © 2025 ESO Maintainer Team

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package template

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"os"
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	corev1 "k8s.io/api/core/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

	esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
)

const (
	pkcs12ContentNoPass   = `MIIJYQIBAzCCCScGCSqGSIb3DQEHAaCCCRgEggkUMIIJEDCCA8cGCSqGSIb3DQEHBqCCA7gwggO0AgEAMIIDrQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQInZmyWpNTPS4CAggAgIIDgPzZTmogBRiLP0NJZEUghZ3Oh1aqHJJ32HKgXUpD5BJ/5AvpUL9FC7m6a3GD++P1On/35J9N50bDjfBJjJrl2zpA143bzltPQBOK30cBJjNsCeN2Dq1dcsvJZfEy20z75NduXjMF6/qs4BbE+1E6nYFYVNHUybFnaQwSx7+2/2OMbXbcFpt4bv3HTw0YLw2pZeW/4/4A9d+tC9UdVQTTyNbI8l9nf1aeaaPsw1keVLmHurmTihfwh469FvjgwiHUP/P3ZCn1tOpWDR8ck0j+ru6imVP2hn+Kvk6svllmYqo3A5DnDRoF/Cl9R0DAPyS0lw7BeGskgTm7B79mzVitTbzRnIUP+sGJjc1AVghnitfcX4ffv8gq5xWaKGucO/IZXbPBoe7tMhKZmsirKzD4RBhC3nMyrwaHJB6PqUwxMQGMLbuHe7GlWhJAy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w30emc+9T/4m1ZDkpBd
BixHvI/EJ8YK3ta5WdJWKC6hnA==
-----END PRIVATE KEY-----
`
	jwkPubRSA     = `{"kid":"ex","kty":"RSA","key_ops":["sign","verify","wrapKey","unwrapKey","encrypt","decrypt"],"n":"p2VQo8qCfWAZmdWBVaYuYb-a-tWWm78K6Sr9poCvNcmv8rUPSLACxitQWR8gZaSH1DklVkqz-Ed8Cdlf8lkDg4Ex5tkB64jRdC1Uvn4CDpOH6cp-N2s8hTFLqy9_YaDmyQS7HiqthOi9oVjil1VMeWfaAbClGtFt6UnKD0Vb_DvLoWYQSqlhgBArFJi966b4E1pOq5Ad02K8pHBDThlIIx7unibLehhDU6q3DCwNH_OOLx6bgNtmvGYJDd1cywpkLQ3YzNCUPWnfMBJRP3iQP_WI21uP6cvo0DqBPBM4wvVzHbCT0vnIflwkbgEWkq1FprqAitZlop9KjLqzjp9vyQ","e":"AQAB"}`
	jwkPubRSAPKIX = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp2VQo8qCfWAZmdWBVaYu
Yb+a+tWWm78K6Sr9poCvNcmv8rUPSLACxitQWR8gZaSH1DklVkqz+Ed8Cdlf8lkD
g4Ex5tkB64jRdC1Uvn4CDpOH6cp+N2s8hTFLqy9/YaDmyQS7HiqthOi9oVjil1VM
eWfaAbClGtFt6UnKD0Vb/DvLoWYQSqlhgBArFJi966b4E1pOq5Ad02K8pHBDThlI
Ix7unibLehhDU6q3DCwNH/OOLx6bgNtmvGYJDd1cywpkLQ3YzNCUPWnfMBJRP3iQ
P/WI21uP6cvo0DqBPBM4wvVzHbCT0vnIflwkbgEWkq1FprqAitZlop9KjLqzjp9v
yQIDAQAB
-----END PUBLIC KEY-----
`
	jwkPrivRSA      = `{"kty" : "RSA","kid" : "cc34c0a0-bd5a-4a3c-a50d-a2a7db7643df","use" : "sig","n"   : "pjdss8ZaDfEH6K6U7GeW2nxDqR4IP049fk1fK0lndimbM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2YTHkaoJ3ncvtvg=
-----END PRIVATE KEY-----
`
	jwkPubEC     = `{"kid":"https://kv-test-mj.vault.azure.net/keys/ec-p-521/e3d0e9c179b54988860c69c6ae172c65","kty":"EC","key_ops":["sign","verify"],"crv":"P-521","x":"AedOAtb7H7Oz1C_cPKI_R4CN_eai5nteY6KFW07FOoaqgQfVCSkQDK22fCOiMT_28c8LZYJRsiIFz_IIbQUW7bXj","y":"AOnchHnmBphIWXvanmMAmcCDkaED6ycW8GsAl9fQ43BMVZTqcTkJYn6vGnhn7MObizmkNSmgZYTwG-vZkIg03HHs"}`
	jwkPubECPKIX = `-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQB504C1vsfs7PUL9w8oj9HgI395qLm
e15jooVbTsU6hqqBB9UJKRAMrbZ8I6IxP/bxzwtlglGyIgXP8ghtBRbtteMA6dyE
eeYGmEhZe9qeYwCZwIORoQPrJxbwawCX19DjcExVlOpxOQlifq8aeGfsw5uLOaQ1
KaBlhPAb69mQiDTccew=
-----END PUBLIC KEY-----
`
	jwkPrivEC      = `{"kty": "EC","kid": "rie3pHe8u8gjSa0IaJfqk7_iEfHeYfDYx-Bqi7vQc0s","crv": "P-256","x": "fDjg3Nq4jPf8IOZ0277aPVal_8iXySnzLUJAZghUzZM","y": "d863PeyBOK_Q4duiSmWwgIRzi1RPlFZTR-vACMlPg-Q","d": "jJs5xsoHUetdMabtt8H2KyX5T92nGul1chFeMT5hlr0"}`
	jwkPrivECPKCS8 = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgjJs5xsoHUetdMabt
t8H2KyX5T92nGul1chFeMT5hlr2hRANCAAR8OODc2riM9/wg5nTbvto9VqX/yJfJ
KfMtQkBmCFTNk3fOtz3sgTiv0OHbokplsICEc4tUT5RWU0frwAjJT4Pk
-----END PRIVATE KEY-----
`
	rsaDecryptPKRSAPKCS8 = `-----BEGIN PRIVATE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cxhFz5Db8qFXBsA+GgXjByQuVbBw04SCKHgc0zhbcWonV
3Rk03pjVB1HfuxcDRja8JZontfMAJyPNJovPu3rIi8npSC+T5g7Fq9UCbQIDAQAB
AoGAAo2+MiKT63GejmYro6g9taf+syJVh9gf/1F7ikzm70jwC5X5rszQ2sXMwcmQ
0izH/nJvnT0VCWOCVwMUPg3a9+odoMNFyg2u3XCLBNr3vlgG3xeCTdjzaMnY61ct
xU4JgpIuiAlwCqhNfKuHxeesM/cvh9eC11ELXh27gLsNCEECQQDng5klIGPLHfiN
3wam6wxLnqHPuhXAyrOAKA1qBlZGKI6n6iBYpfN+Y70gt10f3SBlFfkSyF7uZsUy
maofmjARAkEA0KM6Rj+p2CRMFMh4NpON4RKaQIYNGMTpe/akYBOx6wcZy0saON9l
eHS3nq77TDT+mA3uDbu+6VD8j8eEcXUInQJAYJkfQEd4fBrAR+nj66etVKwW1gbN
5shtBy8vEasdOl7XzyY4YuSzaWwSUOFRYOcyChuV9olWWuDUrR1Cx7bdEQJBALzY
gg7D4UA62oKVUfpUZL+szuJIc+JPmecSwIYWTZymuLpCKGICEx6Mxwdi6yN3dFq9
gRP9NDiLjY+20DLB9CECQB5IqCvT396rjJn3g6sRXHX5qApJwInofLByafcjGd34
ejJKh20FmJegJhkImmNTokNbQZbYiLAP07Ykx9A8jLg=
-----END RSA PRIVATE KEY-----

`
	rsaDecryptDataPKCS1Base64 = `Xd9Jij8+hTqM7ii1nnKbKZy7pHhn3BJwxrENwIlvf0iRysVKn7gmAaD6UV4EpNwYOHvLbo6yLWBme6msVAhIV9KOp22jDe9j837C48rcUiF93Jb7+plabbwTQt4iqi1EKxEfVvKi4tLsLBRhu0v583oQAfCf5aLwF3Vb5bPgGeY=`
	rsaDecryptPubKeyRSAPKCS1  = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8ronsBTX6GD5YhoE/v76+ZkWX
0gODzAD+aCYIyTs4PiWruxlVSOtjwq2gRgUexE5Hsz8cxhFz5Db8qFXBsA+GgXjB
yQuVbBw04SCKHgc0zhbcWonV3Rk03pjVB1HfuxcDRja8JZontfMAJyPNJovPu3rI
i8npSC+T5g7Fq9UCbQIDAQAB
-----END PUBLIC KEY-----

`
)

func rsaEncryptOAEP(t testing.TB, publicKeyPEM []byte, hash, plaintext string) []byte {
	t.Helper()
	block, _ := pem.Decode(publicKeyPEM)
	if block == nil || block.Type != "PUBLIC KEY" {
		t.Fatalf("failed to decode PEM block containing public key")
	}

	pub, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		t.Fatalf("failed to parse DER encoded public key: %v", err)
	}

	rsaPub, ok := pub.(*rsa.PublicKey)
	if !ok {
		t.Fatalf("not RSA public key")
	}
	ciphertext, err := rsa.EncryptOAEP(getHash(hash), rand.Reader, rsaPub, []byte(plaintext), nil)
	require.NoError(t, err)
	return ciphertext
}

func TestExecute(t *testing.T) {
	tbl := []struct {
		name                string
		tpl                 map[string][]byte
		labelsTpl           map[string][]byte
		annotationsTpl      map[string][]byte
		stringDataTpl       map[string][]byte
		data                map[string][]byte
		expectedData        map[string][]byte
		expectedStringData  map[string]string
		expectedLabels      map[string]string
		expectedAnnotations map[string]string
		leftDelimiter       string
		rightDelimiter      string
		expErr              string
		expLblErr           string
		expAnnoErr          string
		expStrErr           string
	}{
		{
			name:           "test empty",
			tpl:            nil,
			labelsTpl:      nil,
			annotationsTpl: nil,
			data:           nil,
		},
		{
			name: "b64dec func",
			tpl: map[string][]byte{
				"foo": []byte("{{ .secret | b64dec }}"),
			},
			data: map[string][]byte{
				"secret": []byte("MTIzNA=="),
			},
			expectedData: map[string][]byte{
				"foo": []byte("1234"),
			},
		},
		{
			name: "fromJson func",
			tpl: map[string][]byte{
				"foo": []byte("{{ $var := .secret | fromJson }}{{ $var.foo }}"),
			},
			data: map[string][]byte{
				"secret": []byte(`{"foo": "bar"}`),
			},
			expectedData: map[string][]byte{
				"foo": []byte("bar"),
			},
		},
		{
			name: "from & toJson func",
			tpl: map[string][]byte{
				"foo": []byte("{{ $var := .secret | fromJson }}{{ $var.foo | toJson }}"),
			},
			data: map[string][]byte{
				"secret": []byte(`{"foo": {"baz":"bang"}}`),
			},
			expectedData: map[string][]byte{
				"foo": []byte(`{"baz":"bang"}`),
			},
		},
		{
			name: "fromJson & toYaml func",
			tpl: map[string][]byte{
				"foo": []byte("{{ $var := .secret | fromJson | toYaml }}{{ $var }}"),
			},
			data: map[string][]byte{
				"secret": []byte(`{"foo": "bar"}`),
			},
			expectedData: map[string][]byte{
				"foo": []byte(`foo: bar`),
			},
		},
		{
			name: "fromYaml & toJson func",
			tpl: map[string][]byte{
				"foo": []byte("{{ $var := .secret | fromYaml | toJson }}{{ $var }}"),
			},
			data: map[string][]byte{
				"secret": []byte(`foo: bar`),
			},
			expectedData: map[string][]byte{
				"foo": []byte(`{"foo":"bar"}`),
			},
		},
		{
			name: "use sprig functions",
			tpl: map[string][]byte{
				"foo": []byte(`{{ .path | ext }}`),
			},
			data: map[string][]byte{
				"path": []byte(`foo/bar/baz.exe`),
			},
			expectedData: map[string][]byte{
				"foo": []byte(`.exe`),
			},
		},
		{
			name: "use replace function",
			tpl: map[string][]byte{
				"foo": []byte(`{{ .conn | replace "postgres://" "db+postgresql://"}}`),
			},
			data: map[string][]byte{
				"conn": []byte(`postgres://user:pass@db.host:5432/dbname`),
			},
			expectedData: map[string][]byte{
				"foo": []byte(`db+postgresql://user:pass@db.host:5432/dbname`),
			},
		},
		{
			name: "use upper function",
			tpl: map[string][]byte{
				"foo": []byte(`{{ .value | upper }}`),
			},
			data: map[string][]byte{
				"value": []byte(`username`),
			},
			expectedData: map[string][]byte{
				"foo": []byte(`USERNAME`),
			},
		},
		{
			name: "multiline template",
			tpl: map[string][]byte{
				"cfg": []byte(`
		datasources:
		- name: Graphite
			type: graphite
			access: proxy
			url: http://localhost:8080
			password: "{{ .password }}"
			user: "{{ .user }}"`),
			},
			data: map[string][]byte{
				"user":     []byte(`foobert`),
				"password": []byte("harharhar"),
			},
			expectedData: map[string][]byte{
				"cfg": []byte(`
		datasources:
		- name: Graphite
			type: graphite
			access: proxy
			url: http://localhost:8080
			password: "harharhar"
			user: "foobert"`),
			},
		},
		{
			name: "base64 pipeline",
			tpl: map[string][]byte{
				"foo": []byte(`{{ "123412341234" | b64enc | b64dec }}`),
			},
			data: map[string][]byte{},
			expectedData: map[string][]byte{
				"foo": []byte("123412341234"),
			},
		},
		{
			name: "base64 pkcs12 extract",
			tpl: map[string][]byte{
				"key":  []byte(`{{ .secret | b64dec | pkcs12key }}`),
				"cert": []byte(`{{ .secret | b64dec | pkcs12cert }}`),
			},
			data: map[string][]byte{
				"secret": []byte(pkcs12ContentNoPass),
			},
			expectedData: map[string][]byte{
				"key":  []byte(pkcs12Key),
				"cert": []byte(pkcs12Cert),
			},
		},
		{
			name: "base64 pkcs12 extract with password",
			tpl: map[string][]byte{
				"key":  []byte(`{{ .secret | b64dec | pkcs12keyPass "123456" }}`),
				"cert": []byte(`{{ .secret | b64dec | pkcs12certPass "123456" }}`),
			},
			data: map[string][]byte{
				"secret": []byte(pkcs12ContentWithPass),
			},
			expectedData: map[string][]byte{
				"key":  []byte(pkcs12Key),
				"cert": []byte(pkcs12Cert),
			},
		},
		{
			name: "base64 decode error",
			tpl: map[string][]byte{
				"key": []byte(`{{ .example | b64dec }}`),
			},
			data: map[string][]byte{
				"example": []byte("iam_no_base64"),
			},
			expErr: "", // silent error
		},
		{
			name: "pkcs12 key wrong password",
			tpl: map[string][]byte{
				"key": []byte(`{{ .secret | b64dec | pkcs12keyPass "wrong" }}`),
			},
			data: map[string][]byte{
				"secret": []byte(pkcs12ContentWithPass),
			},
			expErr: "unable to decode pkcs12",
		},
		{
			name: "pkcs12 cert wrong password",
			tpl: map[string][]byte{
				"cert": []byte(`{{ .secret | b64dec | pkcs12certPass "wrong" }}`),
			},
			data: map[string][]byte{
				"secret": []byte(pkcs12ContentWithPass),
			},
			expErr: "unable to decode pkcs12",
		},
		{
			name: "fromJson error",
			tpl: map[string][]byte{
				"key": []byte(`{{ "{ # no json # }" | fromJson }}`),
			},
			data:   map[string][]byte{},
			expErr: "", // silent error
		},
		{
			name: "template syntax error",
			tpl: map[string][]byte{
				"key": []byte(`{{ #xx }}`),
			},
			data:   map[string][]byte{},
			expErr: "unable to parse template",
		},
		{
			name: "unknown key error",
			tpl: map[string][]byte{
				"key": []byte(`{{ .unknown }}`),
			},
			data:   map[string][]byte{},
			expErr: "unable to execute template at key key",
		},
		{
			name: "jwk rsa pub pem",
			tpl: map[string][]byte{
				"fn": []byte(`{{ .secret | jwkPublicKeyPem }}`),
			},
			data: map[string][]byte{
				"secret": []byte(jwkPubRSA),
			},
			expectedData: map[string][]byte{
				"fn": []byte(jwkPubRSAPKIX),
			},
		},
		{
			name: "jwk rsa priv pem",
			tpl: map[string][]byte{
				"fn": []byte(`{{ .secret | jwkPrivateKeyPem }}`),
			},
			data: map[string][]byte{
				"secret": []byte(jwkPrivRSA),
			},
			expectedData: map[string][]byte{
				"fn": []byte(jwkPrivRSAPKCS8),
			},
		},
		{
			name: "jwk ecdsa pub pem",
			tpl: map[string][]byte{
				"fn": []byte(`{{ .secret | jwkPublicKeyPem }}`),
			},
			data: map[string][]byte{
				"secret": []byte(jwkPubEC),
			},
			expectedData: map[string][]byte{
				"fn": []byte(jwkPubECPKIX),
			},
		},
		{
			name: "jwk ecdsa priv pem",
			tpl: map[string][]byte{
				"fn": []byte(`{{ .secret | jwkPrivateKeyPem }}`),
			},
			data: map[string][]byte{
				"secret": []byte(jwkPrivEC),
			},
			expectedData: map[string][]byte{
				"fn": []byte(jwkPrivECPKCS8),
			},
		},
		{
			name: "filter pem certificate",
			tpl: map[string][]byte{
				"fn": []byte(`{{ .secret | filterPEM "CERTIFICATE" }}`),
			},
			data: map[string][]byte{
				"secret": []byte(jwkPrivRSAPKCS8 + pkcs12Cert),
			},
			expectedData: map[string][]byte{
				"fn": []byte(pkcs12Cert),
			},
		},
		{
			name: "labels",
			tpl: map[string][]byte{
				"foo": []byte("{{ .secret | b64dec }}"),
			},
			labelsTpl: map[string][]byte{
				"bar": []byte("{{ .env | b64dec }}"),
			},
			data: map[string][]byte{
				"secret": []byte("MTIzNA=="),
				"env":    []byte("ZGV2"),
			},
			expectedData: map[string][]byte{
				"foo": []byte("1234"),
			},
			expectedLabels: map[string]string{
				"bar": "dev",
			},
		},
		{
			name: "annotations",
			tpl: map[string][]byte{
				"foo": []byte("{{ .secret | b64dec }}"),
			},
			annotationsTpl: map[string][]byte{
				"bar": []byte("{{ .env | b64dec }}"),
			},
			data: map[string][]byte{
				"secret": []byte("MTIzNA=="),
				"env":    []byte("ZGV2"),
			},
			expectedData: map[string][]byte{
				"foo": []byte("1234"),
			},
			expectedAnnotations: map[string]string{
				"bar": "dev",
			},
		},
		{
			name: "stringData",
			stringDataTpl: map[string][]byte{
				"foo": []byte("{{ .secret | b64dec }}"),
			},
			data: map[string][]byte{
				"secret": []byte("MTIzNA=="),
				"env":    []byte("ZGV2"),
			},
			expectedStringData: map[string]string{
				"foo": "1234",
			},
		},
		{
			name: "NonStandardDelimiters",
			stringDataTpl: map[string][]byte{
				"foo": []byte("<< .secret | b64dec >>"),
			},
			leftDelimiter:  "<<",
			rightDelimiter: ">>",
			data: map[string][]byte{
				"secret": []byte("MTIzNA=="),
				"env":    []byte("ZGV2"),
			},
			expectedStringData: map[string]string{
				"foo": "1234",
			},
		},
		{
			name: "rsa decrypt rsa-oaep sha1 pkcs8 data base64",
			tpl: map[string][]byte{
				"data_decrypted": []byte(`{{ .private_key | rsaDecrypt "RSA-OAEP" "SHA1" (.data_crypted_base64 | b64dec) }}`),
			},
			data: map[string][]byte{
				"private_key":         []byte(rsaDecryptPKRSAPKCS8),
				"data_crypted_base64": []byte(rsaDecryptDataPKCS8Base64),
			},
			expectedData: map[string][]byte{
				"data_decrypted": []byte("a1b2c3d4"),
			},
		},
		{
			name: "rsa decrypt rsa-oaep sha256 pkcs1 data base64",
			tpl: map[string][]byte{
				"data_decrypted": []byte(`{{ .private_key | rsaDecrypt "RSA-OAEP" "SHA256" (.data_crypted_base64 | b64dec) }}`),
			},
			data: map[string][]byte{
				"private_key":         []byte(rsaDecryptPKRSAPKCS1),
				"data_crypted_base64": []byte(rsaDecryptDataPKCS1Base64),
			},
			expectedData: map[string][]byte{
				"data_decrypted": []byte("hellopkcs1sha256"),
			},
		},
		{
			name: "rsa decrypt rsa-oaep sha256 pkcs1 data bin",
			tpl: map[string][]byte{
				"data_decrypted": []byte(`{{ .private_key | rsaDecrypt "RSA-OAEP" "SHA256" .data_crypted_bin }}`),
			},
			data: map[string][]byte{
				"private_key":      []byte(rsaDecryptPKRSAPKCS1),
				"data_crypted_bin": rsaEncryptOAEP(t, []byte(rsaDecryptPubKeyRSAPKCS1), "SHA256", "hellopkcs1sha256"),
			},
			expectedData: map[string][]byte{
				"data_decrypted": []byte("hellopkcs1sha256"),
			},
		},
	}

	for _, tt := range tbl {
		t.Run(tt.name, func(t *testing.T) {
			sec := &corev1.Secret{
				Data:       make(map[string][]byte),
				StringData: make(map[string]string),
				ObjectMeta: v1.ObjectMeta{Labels: make(map[string]string), Annotations: make(map[string]string)},
			}
			oldLeftDelim := leftDelim
			oldRightDelim := rightDelim
			if tt.leftDelimiter != "" {
				leftDelim = tt.leftDelimiter
			}
			if tt.rightDelimiter != "" {
				rightDelim = tt.rightDelimiter
			}
			defer func() {
				leftDelim = oldLeftDelim
				rightDelim = oldRightDelim
			}()
			err := Execute(tt.tpl, tt.data, esapi.TemplateScopeValues, esapi.TemplateTargetData, sec)
			if !ErrorContains(err, tt.expErr) {
				t.Errorf("unexpected error: %s, expected: %s", err, tt.expErr)
			}
			err = Execute(tt.labelsTpl, tt.data, esapi.TemplateScopeValues, esapi.TemplateTargetLabels, sec)
			if !ErrorContains(err, tt.expLblErr) {
				t.Errorf("unexpected error: %s, expected: %s", err, tt.expErr)
			}
			err = Execute(tt.annotationsTpl, tt.data, esapi.TemplateScopeValues, esapi.TemplateTargetAnnotations, sec)
			if !ErrorContains(err, tt.expAnnoErr) {
				t.Errorf("unexpected error: %s, expected: %s", err, tt.expErr)
			}
			if tt.expectedData != nil {
				assert.EqualValues(t, tt.expectedData, sec.Data)
			}
			if tt.expectedLabels != nil {
				assert.EqualValues(t, tt.expectedLabels, sec.ObjectMeta.Labels)
			}
			if tt.expectedAnnotations != nil {
				assert.EqualValues(t, tt.expectedAnnotations, sec.ObjectMeta.Annotations)
			}
		})
	}
}

func TestScopeValuesWithSecretFieldsNil(t *testing.T) {
	tbl := []struct {
		name               string
		tpl                map[string][]byte
		target             string
		data               map[string][]byte
		expectedData       map[string][]byte
		expectedStringData map[string]string
		expErr             string
	}{
		{
			name:   "test empty",
			tpl:    map[string][]byte{},
			target: esapi.TemplateTargetData,
			data:   nil,
		},
		{
			name:   "test byte",
			tpl:    map[string][]byte{"foo": []byte("bar")},
			target: esapi.TemplateTargetData,
			data: map[string][]byte{
				"key":   []byte("foo"),
				"value": []byte("bar"),
			},
			expectedData: map[string][]byte{
				"foo": []byte("bar"),
			},
		},
		{
			name:   "test Annotations",
			tpl:    map[string][]byte{"foo": []byte("bar")},
			target: esapi.TemplateTargetAnnotations,
			data: map[string][]byte{
				"key":   []byte("foo"),
				"value": []byte("bar"),
			},
			expectedStringData: map[string]string{
				"foo": "bar",
			},
		},
		{
			name:   "test Labels",
			tpl:    map[string][]byte{"foo": []byte("bar")},
			target: esapi.TemplateTargetLabels,
			data: map[string][]byte{
				"key":   []byte("foo"),
				"value": []byte("bar"),
			},
			expectedStringData: map[string]string{
				"foo": "bar",
			},
		},
	}
	for i := range tbl {
		row := tbl[i]
		t.Run(row.name, func(t *testing.T) {
			sec := &corev1.Secret{}
			err := Execute(row.tpl, row.data, esapi.TemplateScopeValues, row.target, sec)
			if !ErrorContains(err, row.expErr) {
				t.Errorf("unexpected error: %s, expected: %s", err, row.expErr)
			}
			switch row.target {
			case esapi.TemplateTargetData:
				if row.expectedData != nil {
					assert.EqualValues(t, row.expectedData, sec.Data)
				}
			case esapi.TemplateTargetLabels:
				if row.expectedStringData != nil {
					assert.EqualValues(t, row.expectedStringData, sec.Labels)
				}
			case esapi.TemplateTargetAnnotations:
				if row.expectedStringData != nil {
					assert.EqualValues(t, row.expectedStringData, sec.Annotations)
				}
			}
		})
	}
}

func TestExecuteInvalidTemplateScope(t *testing.T) {
	sec := &corev1.Secret{}
	err := Execute(map[string][]byte{"foo": []byte("bar")}, nil, "invalid", esapi.TemplateTargetData, sec)
	require.Error(t, err)
	assert.ErrorContains(t, err, "expected 'Values' or 'KeysAndValues'")
}

func TestScopeKeysAndValues(t *testing.T) {
	tbl := []struct {
		name               string
		tpl                map[string][]byte
		target             string
		data               map[string][]byte
		expectedData       map[string][]byte
		expectedStringData map[string]string
		expErr             string
	}{
		{
			name:   "test empty",
			tpl:    map[string][]byte{"literal": []byte("")},
			target: "Data",
			data:   nil,
		},
		{
			name:   "test base64",
			tpl:    map[string][]byte{"literal": []byte("{{ .key }}: {{ .value }}")},
			target: esapi.TemplateTargetData,
			data: map[string][]byte{
				"key":   []byte("foo"),
				"value": []byte("bar"),
			},
			expectedData: map[string][]byte{
				"foo": []byte("bar"),
			},
		},
		{
			name:   "test Annotations",
			tpl:    map[string][]byte{"literal": []byte("{{ .key }}: {{ .value }}")},
			target: esapi.TemplateTargetAnnotations,
			data: map[string][]byte{
				"key":   []byte("foo"),
				"value": []byte("bar"),
			},
			expectedStringData: map[string]string{
				"foo": "bar",
			},
		},
		{
			name:   "test Labels",
			tpl:    map[string][]byte{"literal": []byte("{{ .key }}: {{ .value }}")},
			target: esapi.TemplateTargetLabels,
			data: map[string][]byte{
				"key":   []byte("foo"),
				"value": []byte("bar"),
			},
			expectedStringData: map[string]string{
				"foo": "bar",
			},
		},
	}
	for i := range tbl {
		row := tbl[i]
		t.Run(row.name, func(t *testing.T) {
			sec := &corev1.Secret{
				Data:       make(map[string][]byte),
				StringData: make(map[string]string),
				ObjectMeta: v1.ObjectMeta{Labels: make(map[string]string), Annotations: make(map[string]string)},
			}
			err := Execute(row.tpl, row.data, esapi.TemplateScopeKeysAndValues, row.target, sec)
			if !ErrorContains(err, row.expErr) {
				t.Errorf("unexpected error: %s, expected: %s", err, row.expErr)
			}
			switch row.target {
			case esapi.TemplateTargetData:
				if row.expectedData != nil {
					assert.EqualValues(t, row.expectedData, sec.Data)
				}
			case esapi.TemplateTargetLabels:
				if row.expectedStringData != nil {
					assert.EqualValues(t, row.expectedStringData, sec.Labels)
				}
			case esapi.TemplateTargetAnnotations:
				if row.expectedStringData != nil {
					assert.EqualValues(t, row.expectedStringData, sec.Annotations)
				}
			}
		})
	}
}
func TestComplexYAMLFieldsWithSpec(t *testing.T) {
	// These tests verify that the template engine can handle complex YAML values
	// for spec fields. Since ConfigMap doesn't have a spec field in its typed definition,
	// we test by inspecting what would be set in an unstructured representation.

	type testCase struct {
		name   string
		tpl    map[string][]byte
		target string
		scope  esapi.TemplateScope
		data   map[string][]byte
		verify func(t *testing.T, obj *corev1.Secret)
		expErr string
	}

	tests := []testCase{
		{
			name:   "data target with simple string in Secret",
			target: "data",
			scope:  esapi.TemplateScopeValues,
			tpl: map[string][]byte{
				"simple": []byte("{{ .value }}"),
			},
			data: map[string][]byte{
				"value": []byte("test-value"),
			},
			verify: func(t *testing.T, obj *corev1.Secret) {
				assert.Equal(t, []byte("test-value"), obj.Data["simple"])
			},
		},
		{
			name:   "data target with multiple keys",
			target: "data",
			scope:  esapi.TemplateScopeValues,
			tpl: map[string][]byte{
				"username": []byte("{{ .user }}"),
				"password": []byte("{{ .pass }}"),
			},
			data: map[string][]byte{
				"user": []byte("admin"),
				"pass": []byte("secret123"),
			},
			verify: func(t *testing.T, obj *corev1.Secret) {
				assert.Equal(t, []byte("admin"), obj.Data["username"])
				assert.Equal(t, []byte("secret123"), obj.Data["password"])
			},
		},
		{
			name:   "labels target with templated values",
			target: "labels",
			scope:  esapi.TemplateScopeValues,
			tpl: map[string][]byte{
				"app":     []byte("{{ .app }}"),
				"version": []byte("{{ .version }}"),
			},
			data: map[string][]byte{
				"app":     []byte("my-app"),
				"version": []byte("1.0.0"),
			},
			verify: func(t *testing.T, obj *corev1.Secret) {
				assert.Equal(t, "my-app", obj.Labels["app"])
				assert.Equal(t, "1.0.0", obj.Labels["version"])
			},
		},
		{
			name:   "annotations target with templated values",
			target: "annotations",
			scope:  esapi.TemplateScopeValues,
			tpl: map[string][]byte{
				"description": []byte("{{ .desc }}"),
				"owner":       []byte("{{ .owner }}"),
			},
			data: map[string][]byte{
				"desc":  []byte("Test secret"),
				"owner": []byte("team-platform"),
			},
			verify: func(t *testing.T, obj *corev1.Secret) {
				assert.Equal(t, "Test secret", obj.Annotations["description"])
				assert.Equal(t, "team-platform", obj.Annotations["owner"])
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			obj := &corev1.Secret{
				ObjectMeta: v1.ObjectMeta{
					Name:        "test-secret",
					Namespace:   "default",
					Labels:      make(map[string]string),
					Annotations: make(map[string]string),
				},
				Data: make(map[string][]byte),
			}

			err := Execute(tt.tpl, tt.data, tt.scope, tt.target, obj)

			if tt.expErr != "" {
				require.Error(t, err)
				assert.Contains(t, err.Error(), tt.expErr)
				return
			}

			require.NoError(t, err)
			if tt.verify != nil {
				tt.verify(t, obj)
			}
		})
	}
}

func TestTryParseYAML(t *testing.T) {
	tests := []struct {
		name     string
		input    any
		expected any
	}{
		{
			name:     "parse YAML object",
			input:    "key: value\nfoo: bar",
			expected: map[string]any{"key": "value", "foo": "bar"},
		},
		{
			name:     "parse YAML array",
			input:    "- item1\n- item2\n- item3",
			expected: []any{"item1", "item2", "item3"},
		},
		{
			name:     "parse YAML number",
			input:    "42",
			expected: int64(42),
		},
		{
			name:     "parse YAML boolean true",
			input:    "true",
			expected: true,
		},
		{
			name:     "parse YAML boolean false",
			input:    "false",
			expected: false,
		},
		{
			name:     "parse YAML float",
			input:    "3.14",
			expected: 3.14,
		},
		{
			name:     "parse complex YAML",
			input:    "database:\n  host: localhost\n  port: 5432\n  enabled: true",
			expected: map[string]any{"database": map[string]any{"host": "localhost", "port": int64(5432), "enabled": true}},
		},
		{
			name:     "plain string returns as-is",
			input:    "just a string",
			expected: "just a string",
		},
		{
			name:     "invalid YAML returns original",
			input:    "invalid: : yaml:",
			expected: "invalid: : yaml:",
		},
		{
			name:     "non-string input returns as-is",
			input:    123,
			expected: 123,
		},
		{
			name:     "byte slice returns as-is",
			input:    []byte("test"),
			expected: []byte("test"),
		},
		{
			name:     "null/empty string",
			input:    "",
			expected: nil,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := tryParseYAML(tt.input)
			assert.Equal(t, tt.expected, result)
		})
	}
}

func ErrorContains(out error, want string) bool {
	if out == nil {
		return want == ""
	}
	if want == "" {
		return false
	}
	return strings.Contains(out.Error(), want)
}

func TestPkcs12certPass(t *testing.T) {
	const (
		leafCertPath         = "_testdata/foo.crt"
		intermediateCertPath = "_testdata/intermediate-ca.crt"
		rootCertPath         = "_testdata/root-ca.crt"
		disjunctCertPath     = "_testdata/disjunct-root-ca.crt"
	)
	type args struct {
		pass     string
		filename string
	}
	type testCase struct {
		name    string
		args    args
		want    []string
		wantErr bool
	}
	tests := []testCase{
		{
			// this case expects the whole chain to be stored
			// in a single bag.
			// bag(1): leaf/root/intermediate cert
			// bag(2): private key
			name: "read file without password",
			args: args{
				pass:     "",
				filename: "_testdata/foo-nopass.pfx",
			},
			want: []string{
				// this order is important
				leafCertPath,
				intermediateCertPath,
				rootCertPath,
			},
		},
		{
			// same as above but with password
			name: "read file with password",
			args: args{
				pass:     "1234",
				filename: "_testdata/foo-withpass-1234.pfx",
			},
			want: []string{
				// this order is important
				leafCertPath,
				intermediateCertPath,
				rootCertPath,
			},
		},
		{
			// cert chain may be stored in different bags
			// this test case uses a pfx that has the following structure:
			// bag(1): leaf certificate
			// bag(2): root + intermediate cert
			// bag(3): private key
			name: "read multibag cert chain",
			args: args{
				pass:     "",
				filename: "_testdata/foo-multibag-nopass.pfx",
			},
			want: []string{
				// this order is important
				leafCertPath,
				intermediateCertPath,
				rootCertPath,
			},
		},
		{
			// cert chain may contain a disjunct cert
			// bag(1): leaf/root/intermediate/disjunct
			// bag(2): private key
			name: "read disjunct cert chain",
			args: args{
				pass:     "",
				filename: "_testdata/foo-disjunct-nopass.pfx",
			},
			want: []string{
				// this order is important
				leafCertPath,
				rootCertPath,
				intermediateCertPath,
				disjunctCertPath,
			},
		},
		{
			name: "read file wrong password",
			args: args{
				pass:     "wrongpass",
				filename: "_testdata/foo-withpass-1234.pfx",
			},
			wantErr: true,
		},
	}

	testFunc := func(t *testing.T, tc testCase) {
		archive, err := os.ReadFile(tc.args.filename)
		if err != nil {
			t.Error(err)
		}
		var expOut []byte
		for _, w := range tc.want {
			c, err := os.ReadFile(w)
			if err != nil {
				t.Error(err)
			}
			expOut = append(expOut, c...)
		}
		got, err := pkcs12certPass(tc.args.pass, string(archive))
		if (err != nil) != tc.wantErr {
			t.Errorf("pkcs12certPass() error = %v, wantErr %v", err, tc.wantErr)
			return
		}
		if diff := cmp.Diff(string(expOut), got); diff != "" {
			t.Errorf("pkcs12certPass() = diff:\n%s", diff)
		}
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			testFunc(t, tt)
		})
	}
}

func TestConfigMapDataNotBase64Encoded(t *testing.T) {
	configMap := &corev1.ConfigMap{
		ObjectMeta: v1.ObjectMeta{
			Name:      "test-cm",
			Namespace: "default",
		},
	}

	data := map[string][]byte{
		"host":     []byte("localhost"),
		"port":     []byte("5432"),
		"database": []byte("mydb"),
	}

	tplMap := map[string][]byte{
		"host":     []byte("{{ .host }}"),
		"port":     []byte("{{ .port }}"),
		"database": []byte("{{ .database }}"),
	}

	err := Execute(tplMap, data, esapi.TemplateScopeValues, "Data", configMap)
	require.NoError(t, err)

	assert.Equal(t, "localhost", configMap.Data["host"], "host should be plain text, not base64")
	assert.Equal(t, "5432", configMap.Data["port"], "port should be plain text, not base64")
	assert.Equal(t, "mydb", configMap.Data["database"], "database should be plain text, not base64")
}

func TestNestedPathTargeting(t *testing.T) {
	tests := []struct {
		name     string
		target   string
		scope    esapi.TemplateScope
		tpl      map[string][]byte
		data     map[string][]byte
		verify   func(t *testing.T, obj map[string]interface{})
		wantErr  bool
		errorMsg string
	}{
		{
			name:   "nested path spec.slack with template variables",
			target: "spec.slack",
			scope:  esapi.TemplateScopeKeysAndValues,
			tpl: map[string][]byte{
				"slack-config": []byte(`
api_url: {{ .url }}
webhook_url: {{ .webhook }}
`),
			},
			data: map[string][]byte{
				"url":     []byte("https://hooks.slack.com/services/XXX"),
				"webhook": []byte("https://hooks.slack.com/services/YYY"),
			},
			verify: func(t *testing.T, obj map[string]interface{}) {
				specMap := obj["spec"].(map[string]interface{})
				slackMap := specMap["slack"].(map[string]interface{})

				// Should NOT have spec.slack.api_url.url (the bug with template variables)
				apiURL := slackMap["api_url"]
				assert.Equal(t, "https://hooks.slack.com/services/XXX", apiURL, "api_url should be string, not nested map")

				webhookURL := slackMap["webhook_url"]
				assert.Equal(t, "https://hooks.slack.com/services/YYY", webhookURL, "webhook_url should be string, not nested map")

				// Verify the fix: should NOT have duplicate 'slack' in the path
				_, hasDuplicateSlack := slackMap["slack"]
				assert.False(t, hasDuplicateSlack, "should not have spec.slack.slack (the bug)")
			},
		},
		{
			name:   "nested path merge behavior - preserves existing fields",
			target: "spec.slack",
			scope:  esapi.TemplateScopeKeysAndValues,
			tpl: map[string][]byte{
				"slack-config": []byte(`
api_url: {{ .url }}
`),
			},
			data: map[string][]byte{
				"url": []byte("https://hooks.slack.com/services/NEW"),
			},
			verify: func(t *testing.T, obj map[string]interface{}) {
				specMap := obj["spec"].(map[string]interface{})
				slackMap := specMap["slack"].(map[string]interface{})

				// Should have the new field from template
				assert.Equal(t, "https://hooks.slack.com/services/NEW", slackMap["api_url"], "api_url should be set from template")

				// Should PRESERVE existing fields that were not in the template
				assert.Equal(t, "general", slackMap["channel"], "existing channel field should be preserved")
				assert.Equal(t, "test-value", slackMap["other_field"], "existing other_field should be preserved")
			},
		},
		{
			name:   "nested path overwrite field - updates existing value",
			target: "spec.slack",
			scope:  esapi.TemplateScopeKeysAndValues,
			tpl: map[string][]byte{
				"slack-config": []byte(`
channel: {{ .new_channel }}
`),
			},
			data: map[string][]byte{
				"new_channel": []byte("alerts"),
			},
			verify: func(t *testing.T, obj map[string]interface{}) {
				specMap := obj["spec"].(map[string]interface{})
				slackMap := specMap["slack"].(map[string]interface{})

				// Should update the existing field
				assert.Equal(t, "alerts", slackMap["channel"], "channel should be updated")

				// Should still preserve other existing fields
				assert.Equal(t, "https://hooks.slack.com/existing", slackMap["api_url"], "existing api_url should be preserved")
				assert.Equal(t, "test-value", slackMap["other_field"], "existing other_field should be preserved")
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Use an unstructured object to test generic target behavior
			obj := &unstructured.Unstructured{
				Object: map[string]interface{}{
					"apiVersion": "example.com/v1",
					"kind":       "TestResource",
					"metadata": map[string]interface{}{
						"name":      "test-resource",
						"namespace": "default",
					},
				},
			}

			// For merge behavior tests, pre-populate the object with existing content
			if strings.Contains(tt.name, "merge behavior") {
				obj.Object["spec"] = map[string]interface{}{
					"slack": map[string]interface{}{
						"channel":     "general",
						"other_field": "test-value",
					},
				}
			}

			// For overwrite field test, pre-populate with different initial values
			if strings.Contains(tt.name, "overwrite field") {
				obj.Object["spec"] = map[string]interface{}{
					"slack": map[string]interface{}{
						"channel":     "general",
						"api_url":     "https://hooks.slack.com/existing",
						"other_field": "test-value",
					},
				}
			}

			err := Execute(tt.tpl, tt.data, tt.scope, tt.target, obj)

			if tt.wantErr {
				require.Error(t, err)
				if tt.errorMsg != "" {
					assert.Contains(t, err.Error(), tt.errorMsg)
				}
			} else {
				require.NoError(t, err)
				if tt.verify != nil {
					tt.verify(t, obj.Object)
				}
			}
		})
	}
}
